package com.ontology.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import com.ontology.util.model.impl.OntologyColumn;
public class OUtils {
	private OUtils() {
	}
	public static List<Row> readExcelFile(File file)
	throws InvalidFormatException, IOException {
		InputStream inp = new FileInputStream(file);
		Workbook wb = WorkbookFactory.create(inp);
		Sheet sheet = wb.getSheetAt(0);
		System.out.println(sheet.getLastRowNum() + " Physical " + sheet.getPhysicalNumberOfRows());
		int rowEnd = sheet.getPhysicalNumberOfRows();
		List<Row> rows = new ArrayList<Row>();
		for (int rowIndex = 0; rowIndex < rowEnd; rowIndex++) {
			rows.add(sheet.getRow(rowIndex));
		}
		return rows;
	}
	public static List<String> tokenizeString(String content, String delim) {
		List<String> tokenized = new ArrayList<String>();
		StringTokenizer stringTokenizer = new StringTokenizer(content, delim);
		while (stringTokenizer.hasMoreTokens()) {
			tokenized.add(stringTokenizer.nextToken());
		}
		return tokenized;
	}
	public static OntologyColumn buildColumn(String name, ODataType type,
	Boolean mandatory, String dateFormat) {
		OntologyColumn column = new OntologyColumn();
		column.setDateFormat(dateFormat == null
		? null
		: new SimpleDateFormat(dateFormat));
		column.setMandatory(mandatory);
		column.setName(name);
		column.setType(type);
		return column;
	}
	public static Double parseDouble(String value) {
		try {
			return Double.parseDouble(value);
		} catch (Exception e) {
			return null;
		}
	}
	public static Long parseLong(String value) {
		try {
			return Long.parseLong(value, 10);
		} catch (Exception e) {
			return null;
		}
	}
	public static Date parseDate(String dateVale) {
		return parseDate(dateVale, OConstants.DEFAULT_DATE_FORMAT);
	}
	public static Date parseDate(String dateVale, SimpleDateFormat format) {
		try {
			return format.parse(dateVale);
		} catch (Exception e) {
			return null;
		}
	}
	public static OntologyColumn buildColumn(String column) {
		int hasExtras = column.indexOf("{");
		OntologyColumn ontologyColumn = new OntologyColumn();
		if (hasExtras < 0) {
			ontologyColumn.setName(column);
			return ontologyColumn;
		}
		ontologyColumn.setName(column.substring(0, hasExtras));
		String restColumn = column.substring(hasExtras);
		StringTokenizer extraTokenizer = new StringTokenizer(restColumn, "{}");
		while (extraTokenizer.hasMoreTokens()) {
			String dataTypeExtra = extraTokenizer.nextToken();
			String typeIdentifer = StringUtils.trimToNull(getTypeIdentifier(dataTypeExtra));
			if (typeIdentifer == null) {
				continue;
			}
			ontologyColumn.setType(OConstants.KEY_AND_DATA_TYPE_MAP.get(typeIdentifer));
			if (OConstants.TYPE_D.equals(typeIdentifer) == false) {
				continue;
			}
			dataTypeExtra = StringUtils.trimToNull(dataTypeExtra.substring(typeIdentifer.length()));
			if (dataTypeExtra == null) {
				continue;
			}
			StringTokenizer dateFormatTokenizer = new StringTokenizer(dataTypeExtra, "[]");
			while (dateFormatTokenizer.hasMoreTokens()) {
				String dateFormat = StringUtils.trimToNull(dateFormatTokenizer.nextToken());
				ontologyColumn.setDateFormat(dateFormat == null
				? null
				: new SimpleDateFormat(dateFormat));
			}
		}
		System.out.println(ontologyColumn);
		return ontologyColumn;
	}
	private static String getTypeIdentifier(String typeExtra) {
		int index = typeExtra.indexOf("[");
		if (index < 0) {
			return typeExtra.toUpperCase();
		}
		return typeExtra.substring(0, index).toUpperCase();
	}
}
